package com.atguigu.tingshu.account.mq;

import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
@Slf4j
public class AccountReceiver {
    @Autowired
    private UserAccountService userAccountService;

    @KafkaListener(topics = KafkaConstant.QUEUE_USER_REGISTER)
    public void initAccount(ConsumerRecord<String,String> record){ //ConsumerRecord对象中封装的就是topic中分区的数据
        String userId = record.value();
        if(!StringUtils.isEmpty(userId)){
            userAccountService.initAccount(Long.parseLong(userId));
            log.info("初始化账户成功...userId: {}" +userId);
        }

    }
    @KafkaListener(topics = KafkaConstant.QUEUE_ACCOUNT_MINUS)
    public void minus(ConsumerRecord<String,String> record){ //ConsumerRecord对象中封装的就是topic中分区的数据
        String orderNo = record.value();
        if(!StringUtils.isEmpty(orderNo)){
            userAccountService.minus(orderNo);
            log.info("初始化账户总金额成功...orderNo: {}" +orderNo);
        }
    }

    @KafkaListener(topics = KafkaConstant.QUEUE_ACCOUNT_UNLOCK)
    public void unlock(ConsumerRecord<String,String> consumerRecord){
        String orderNo = consumerRecord.value();
        if(!StringUtils.isEmpty(orderNo)){
            userAccountService.unlock(orderNo);
            log.info("解锁金额成功...tradeNo: {}" +orderNo);
        }
    }

    @KafkaListener(topics = KafkaConstant.QUEUE_RECHARGE_PAY_SUCCESS)
    public void rechargePaySuccess(ConsumerRecord<String,String> consumerRecord){
        String orderNo = consumerRecord.value();
        if(!StringUtils.isEmpty(orderNo)){
            userAccountService.rechargePaySuccess(orderNo);
            log.info("充值成功...tradeNo: {}" +orderNo);
        }
    }

}
